import json
import sys
import traceback
import requests
from app.common.exceptions import GenericException
from app.common.enums.resp_enum import ErrTypes
from app import conf

import logging
logger = logging.getLogger(__name__)


def get_service_api(url, params=None):
    logger.info('>> get_service_api')
    logger.debug(f'url: {url}')
    try:
        resp = requests.get(url, params)
        #import pdb;pdb.set_trace()
        content = resp.content.decode("utf-8")
        logger.debug(f'resp code: {resp.status_code}, content: {content}')
        respData = json.loads(content)
        logger.info('respData: ', respData)
        return respData
        #return resp.content
    except Exception as err:
        logger.exception(err)
        exc_type, exc_value, exc_obj = sys.exc_info()
        traceback.print_exception(exc_type,exc_value,exc_obj,limit=3,file=sys.stdout)
        raise GenericException(ErrTypes.trans_request_error)

def post_service_api(url, payload):
    logger.info('>> request_service_api')
    logger.debug(f'url: {url}')
    headers = {
        'Content-Type': 'application/json;charset=utf-8'
    }
    try:
        resp = requests.post(url, json=payload, headers=headers)
        #import pdb;pdb.set_trace()
        content = resp.content.decode("utf-8")
        logger.debug(f'resp code: {resp.status_code}, content: {content}')
        respData = json.loads(content)
        logger.info('respData: ', respData)
        return respData
        #return resp.content
    except Exception as err:
        logger.exception(err)
        exc_type, exc_value, exc_obj = sys.exc_info()
        traceback.print_exception(exc_type,exc_value,exc_obj,limit=3,file=sys.stdout)
        raise GenericException(ErrTypes.trans_request_error)

def request_service_api(base_url, uri, payload):
    logger.info('>> request_service_api')
    url = f'{base_url}{uri}'
    logger.debug(f'url: {url}')
    print(f'payload: {json.dumps(payload)}')
    headers = {
        'Content-Type': 'application/json;charset=utf-8'
    }
    try:
        resp = requests.post(url, json=payload, headers=headers)
        #import pdb;pdb.set_trace()
        content = resp.content.decode("utf-8")
        logger.debug(f'resp code: {resp.status_code}, content: {content}')
        respData = json.loads(content)
        logger.info('respData: ', respData)
        return respData
        #return resp.content
    except Exception as err:
        logger.exception(err)
        raise GenericException(ErrTypes.srv_req_error)


def request_fe_api(base_url, uri, payload):
    logger.info('>> request_fe_api')
    url = f'{base_url}{uri}'
    logger.debug(f'url: {url}')
    headers = {
        'Content-Type': 'application/json;charset=utf-8'
    }
    try:
        resp = requests.post(url, json=payload, headers=headers)
        #import pdb;pdb.set_trace()
        content = resp.content.decode("utf-8")
        logger.debug(f'resp code: {resp.status_code}, content: {content}')
        respData = json.loads(content)
        logger.info('respData: ', respData)
        return respData
        #return resp.content
    except Exception as err:
        logger.exception(err)
        raise GenericException(ErrTypes.trans_request_error)

